<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="list"
      :rules="formRules"
      label-width="0px"
      v-loading="formLoading"
    >
      <el-form-item label="退回原因"  label-width="100px">
        <el-input v-model="formData.returnReason" type="textarea" placeholder="请输入退回原因" height="100px" />
      </el-form-item>

  </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>  
</template>
<script setup lang="ts">
import { DepPurchaseItemApi,DepPurchaseItemVO } from '@/api/erp/deppurchaseitem'


const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const queryParams = reactive({
  integrateId: undefined,
  productUnitId: undefined,
  productStandards: undefined,
  supplierId: undefined,
  productName: undefined,
  returnReason: undefined
})
const formData = ref({
  returnReason: undefined
})
const formRules = reactive({
  countActual: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
})

const formRef = ref() // 表单 Ref

/** 查询列表 */


/** 打开弹窗 */
const open1 = async (row: any, integrateId: String) => {
  dialogVisible.value = true
  dialogTitle.value = '退回'
  queryParams.integrateId = integrateId
  queryParams.productUnitId = row.productUnitId
  queryParams.productStandards = row.productStandards
  queryParams.supplierId = row.supplierId
  queryParams.productName = row.productName

  }

  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()

  // 提交请求
  formLoading.value = true
  try {
    queryParams.returnReason = formData.value.returnReason
    await DepPurchaseItemApi.sendBackDepPurchaseItem(queryParams)
    message.success(t('common.sendBackSuccess'))
    
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

defineExpose({ open1 }) // 提供 open 方法，用于打开弹窗

/** 初始化 **/
// onMounted(() => {
//   getList()
// })
</script>